package com.ps.actions.dept;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.ps.actions.BaseAction;
import com.ps.actions.IFBaseAction;
import com.ps.entity.Result;
import com.ps.entity.Status;
import com.ps.pojo.dept.WorkPosition;
import com.ps.service.dept.WorkPositionService;
import com.ps.service.salary.SalarySetOfBooksService;
import com.ps.util.Kit;
import com.ps.util.PaginationSupport;

public class WorkPositionAction extends BaseAction implements IFBaseAction {
	private Integer departmentId;
	private String paginationJson;
	
	private PaginationSupport<?> pageing;
	
	private Integer wpId;
	
	private WorkPosition workPos;
	
	@Autowired
	private WorkPositionService workPositionService;
	
	@Autowired
	private SalarySetOfBooksService salarySetOfBooksService;
	
	private Integer findByPageType;
	
	private String salarySetName;
	
	public String add() {
		workPos.setInputerId(getSessionUserId());
		this.result =  workPositionService.save(workPos);
		System.out.println(Kit.toJson(workPos));
		
		if (Status.SUCCES.equals(this.result.getStatus().getCode())) {
			return "main";
		} else {
			return "add";
		}
		
	}

	public String del() {
		// TODO Auto-generated method stub
		outputJson(Kit.toJson(workPositionService.delete(wpId, this.departmentId)));
		
		return null;
	}

	public String getById() {
		// TODO Auto-generated method stub
		return null;
	}

	public String query() {
		// TODO Auto-generated method stub
		return null;
	}

	public String update() {
		
		this.result =  workPositionService.update(workPos);
		
		
		if (Status.SUCCES.equals(this.result.getStatus().getCode())) {
			
			return "main";
		} else {
			return "upt";
		}
	}

	@SuppressWarnings("unchecked")
	public String findByPage() {
		PaginationSupport paginationSupport
			= Kit.getFromJson(paginationJson, PaginationSupport.class);
		Result result ;
		if (findByPageType == null || findByPageType == 4) {
			result = workPositionService.findByPage(paginationSupport.getPageSize(), paginationSupport.getStartIndex());
		} else {
			result = workPositionService.findByPageFromDept(paginationSupport.getPageSize(), paginationSupport.getStartIndex(), departmentId, findByPageType);
		} 
		outputJson(Kit.toJson(result));
		return null;
	}


	@SuppressWarnings("unchecked")
	public String findBySSByPage() {
		System.out.println(paginationJson);
		PaginationSupport paginationSupport
		    = Kit.getFromJson(paginationJson, PaginationSupport.class);
	    Result result = salarySetOfBooksService.findByPage(paginationSupport.getPageSize(), paginationSupport.getStartIndex());
	    outputJson(Kit.toJson(result));
		return null;
	}
	
    
	

	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setPaginationJson(String paginationJson) {
		this.paginationJson = paginationJson;
	}

	public String getPaginationJson() {
		return paginationJson;
	}

	public String jumpTo() {
		if (StringUtils.equals(this.jump, "add")) {
			
		} else if (StringUtils.equals(this.jump, "upt")) {
			setResult(workPositionService.findById(this.wpId));
		}
		
		return this.jump;
	}

	public void setFindByPageType(Integer findByPageType) {
		this.findByPageType = findByPageType;
	}

	public Integer getFindByPageType() {
		return findByPageType;
	}

	public void setPageing(PaginationSupport<?> pageing) {
		this.pageing = pageing;
	}

	public PaginationSupport<?> getPageing() {
		return pageing;
	}

	public void setWpId(Integer wpId) {
		this.wpId = wpId;
	}

	public Integer getWpId() {
		return wpId;
	}

	public void setWorkPos(WorkPosition workPos) {
		this.workPos = workPos;
	}

	public WorkPosition getWorkPos() {
		return workPos;
	}

	public void setSalarySetName(String salarySetName) {
		this.salarySetName = salarySetName;
	}

	public String getSalarySetName() {
		return salarySetName;
	}



	

}
